﻿using MYQQX.SP;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MYQQX
{
    public class CardGroup
    {
        // 对方
        public CardGroup OtherGroup { get; set; }
        
        // 显示控件
        public CardsList ShowList { get; set; }

        // 卡牌列表
        internal List<Card> cardList = new List<Card>();

        // 组合列表
        internal List<ScoreGroup> Group = new List<ScoreGroup>();

        // 特殊牌列表
        internal List<ISp> spList = new List<ISp>();

        internal int CurrentScore  => CalculateScore();

        /// <summary>
        /// 是否由AI控制
        /// </summary>
        internal bool AI { get; set; }

        private int CalculateScore()
        {
            var score = cardList.Count * 2   // 卡牌基本分
                + Group.Sum(t => t.Score);   // 组合基本分
            // 触发的特殊卡
            var useSpList = spList.Where(t => this.cardList.Any(k => k.Name == t.CardName));
            score += useSpList.Sum(t => t.SpScore); // 特殊卡牌基础分
            score += +useSpList.Sum(t => t.GetExtraScore(this));   // 特殊牌对于卡牌以及组合的额外分
            return score;
        }

        public List<ScoreGroup> AddCard(bool changed = true, params Card[] cards)
        {
            cardList.AddRange(cards);
            var ct = spList.Count;
            for(int i=0;i<ct;i++)
            {
                if (i >= spList.Count)
                    break;
                var sp = spList[i];
                var card = cards.FirstOrDefault(t => t.Name == sp.CardName);
                if (card != null)
                {
                    sp.AI = this.AI;
                    sp.BeChanged = changed;
                    sp.Got(this, OtherGroup);
                }
            }

            return CheckGroup(cardList.ToArray());
        }

        private List<ScoreGroup> CheckGroup(Card[] nCard)
        {
            List<ScoreGroup> NewGroups = new List<ScoreGroup>();
            var gps = GroupConfig.Instance.GetGroups(nCard);
            foreach (var gp in gps)
            {
                if (!this.Group.Contains(gp))
                {
                    if (gp.groups.Intersect(this.cardList.Select(t => t.Name)).Count() == gp.groups.Length)
                    {
                        this.Group.Add(gp);

                        NewGroups.Add(gp);
                    }
                }
            }
            return NewGroups;
        }

        internal ISp GetSpCard(string cCardName)
        {
            return this.spList.FirstOrDefault(t => t.CardName == cCardName);
        }

        internal void AddCardByName(string cardName, bool Changed = false)
        {
            var card = CardConfig.Instance.GetCardByName(cardName);

            this.AddCard(Changed, card);
        }

        internal void RemoveCard(string name)
        {
            this.cardList.RemoveAll(t => t.Name == name);
            // 若有对应的特殊牌，也要特殊牌的效果失效
            var sp = this.spList.FirstOrDefault(t => t.CardName == name);
            if (sp != null)
                sp.Lost();

            this.Group.RemoveAll(t => t.groups.Any(k => k == name));
        }
        internal void RemoveSpCard(string name)
        {
            var sp = this.spList.FirstOrDefault(t => t.Name == name);
            if (sp != null)
            {
                sp.Lost();
                this.spList.Remove(sp);
            }
        }
    }
}